//
//  WLStatusViewModel.swift
//  WLWeibo
//
//  Created by Devil on 16/8/13.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

class WLStatusViewModel: NSObject {
    //创建一个对象model
    var statusModel:WLStatusModel?
    //微博等级图片
    var mbrankImage:UIImage?
    //微博认证图片
    var verifiedImage: UIImage?
    //微博来源
    var sinaSourcceStr: String?
    //转发
    var repostsCountStr: String?
    //评论
    var commentCountStr: String?
    //赞
    var attitudesCountStr: String?
    
    var sinaTimeStr: String?{
    
        return NSDate.getSinaDate(statusModel?.created_at ?? "").sinaDateDescription
    }
    
    init(statusModel:WLStatusModel) {
        super.init()
        self.statusModel = statusModel
        mbrankImage = dealMbrankImage(statusModel.user?.mbrank ?? 0)
        verifiedImage = dealMbrankImage(statusModel.user?.verfied ?? -1)
        sinaSourcceStr = dealSinaSourceStr(statusModel.source ?? "")
        repostsCountStr = getSinaCountStr(statusModel.reposts_count ?? 0, title: "转发")
        commentCountStr = getSinaCountStr(statusModel.reposts_count ?? 0, title: "评论")
        attitudesCountStr = getSinaCountStr(statusModel.reposts_count ?? 0, title: "赞")
        
        
    }
}
//处理底部视图  转发评论赞
extension WLStatusViewModel{

    func getSinaCountStr(count: Int,title: String) -> String{
        //如果 count <=0 直接显示
        if count <= 0 {
            return title
              //如果 count >0 && count < 10000  是多少显示多少
        }else if count > 0 && count < 10000{
        
            return "\(count)"
            
        }else{
            //如果 count >= 10000
            let float = CGFloat(count/1000)/10
            var floatStr = "\(float)万"
            //如果 count%10000 == 0
            if floatStr.containsString(".0") {
                floatStr = floatStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return floatStr
        }
    }
}
//处理微博来源
extension WLStatusViewModel{

    func dealSinaSourceStr(source: String) -> String{
    
        printLog("呵呵哒")
        //判断字符串中是否有">
        if source.containsString("\">") {
            //获取range
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            //字符串截取
            let result = source.substringWithRange(startRange.endIndex..<endRange.startIndex)
            return "来自\(result)"
        }
        //如果字符串为空
        return "来自遥远的M78星"
    }
}
//处理cell中数据逻辑
extension WLStatusViewModel{
    //处理微博等级图片  会员等级1-6
    func dealMbrankImage(mbrank: Int) -> UIImage?{
    
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return UIImage(named: "common_icon_membership")
    }
    //处理微博用户认证图片
    func dealVerifiedImage(verified: Int) -> UIImage?{
    
        switch verified {
        //认证用户
        case 1:
            return UIImage(named: "avatar_vip")
        //企业认证
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        //达人
        case 220:
            return UIImage(named: "avatar_grassroot")
        //没有认证
        default:
            return UIImage(named: "avatar_vgirl")
        }
    
    }

}
